Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to define and update a defaultPath for applications #64498

Merged

Conversation

pgayvallet
Copy link
Contributor

@pgayvallet pgayvallet commented Apr 27, 2020

Summary

Fix #56027

Add a defaultPath property to applications and to their updatable fields, and use it in application.navigateToApp and in the UI navlinks urls.

Checklist

Dev Docs

KP applications can now define and update the defaultPath that should be used when navigating to them from another application or from the navigation bar.

core.application.register({
    id: 'my-app',
    // ...
    defaultPath: '/some-path',
})
const appUpdater = new BehaviorSubject<AppUpdater>(() => ({}));
core.application.register({
    id: 'my-app',
    // ...
    updater$: appUpdater,
})

// later
appUpdater.next(() => ({ defaultPath: '/some-updated-path' }));

@pgayvallet pgayvallet added release_note:skip Skip the PR/issue when compiling release notes v7.8.0 v8.0.0 Feature:New Platform Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc labels Apr 27, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

@pgayvallet pgayvallet added release_note:plugin_api_changes Contains a Plugin API changes section for the breaking plugin API changes section. and removed release_note:skip Skip the PR/issue when compiling release notes labels Apr 27, 2020
* When defined, this value will be used as a default for the `path` option when calling {@link ApplicationStart.navigateToApp | navigateToApp}`,
* and will also be appended to the {@link ChromeNavLink | application navLink} in the navigation bar.
*/
defaultPath?: string;
Copy link
Contributor Author

@pgayvallet pgayvallet Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is on AppBase because AppUpdatableFields is a pick from this root class (and I didn't want to overcomplexify the AppUpdatableFields type with generics). However this property is only used for KP apps. This will be cleaned up 'naturally' once we drop legacy support

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we mark it as @deprecated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not deprecated. It's just that even if on AppBase, it's effectively only usable by the KP App subclass, and not the LegacyApp one.

Comment on lines +30 to +37
const getKibanaUrl = (pathname?: string, search?: string) =>
url.format({
protocol: 'http:',
hostname: process.env.TEST_KIBANA_HOST || 'localhost',
port: process.env.TEST_KIBANA_PORT || '5620',
pathname,
search,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this is the third duplication of this helper in test/plugin_functional/test_suites/core_plugins/* files. I would gladly extract it to an util file, however I'm unsure of the conventions for such files in FTR code. Moving it to a service seems overkill as it's 'stateless'. Any idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* Utility to remove trailing, leading or duplicate slashes.
* By default will only remove duplicates.
*/
export const removeSlashes = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I move this to src/core/utils instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider to move it when needed in another place

@pgayvallet pgayvallet marked this pull request as ready for review April 27, 2020 10:50
@pgayvallet pgayvallet requested a review from a team as a code owner April 27, 2020 10:50
@pgayvallet pgayvallet requested a review from flash1293 April 27, 2020 10:50
Copy link
Contributor

@flash1293 flash1293 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled this into my cutover PR, renamed the property and everything continues to work. LGTM, great job! No detailed code review

@flash1293
Copy link
Contributor

flash1293 commented Apr 28, 2020

@pgayvallet I found a minor issue - in src/core/public/chrome/ui/header/recent_links.tsx the navLink.baseUrl is compared to the href of the recently accessed item. With this PR this doesn't work anymore, because baseUrl in that place will contain the whole defaultPath as well. In this place it should just compare to the actual baseUrl (without the defaultPath part).

@pgayvallet
Copy link
Contributor Author

@flash1293 good catch, thanks. I did not want to introduce a new field, which is why I choose to alter the baseUrl instead, but I guess I will need to go back on that.

@flash1293
Copy link
Contributor

flash1293 commented Apr 28, 2020

@pgayvallet Noticed another problem - I thought it would be irrelevant first, but it seems like it's breaking the history object in some cases. In https://github.com/elastic/kibana/pull/64498/files#diff-1ab7c26e3c5a0d226f771d13620db1e3L88 it's stripping away trailing slashes. But those are required for these cases: /app/discover#/ (and should be kept)

Edit: nvm, seems to be an unrelated problem.

@pgayvallet
Copy link
Contributor Author

@flash1293 the recent links issue should be fixed. I also fixed the trailing / being removed in hashbangs, even if it seems it's not the actual cause of your other issue.

@flash1293
Copy link
Contributor

@pgayvallet Tested the fix and everything works fine now, thanks 👍

expect(MockHistory.push).toHaveBeenCalledWith('/app/app1', undefined);
MockHistory.push.mockClear();

updater.next(app => ({ defaultPath: 'default-path' }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from this example I'd expect it to be called as defaultSubpath / internalPath, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default part is important as the prop is used as a default in navigateToApp.
However in navigateToApp, the app path option is called path, not subPath, so I choose defaultPath.

But I'm fine with defaultSubPath if we think this is more explicit.

it('preserves trailing slash when path contains a hash', async () => {
const { register } = service.setup(setupDeps);

register(Symbol(), createApp({ id: 'app2', appRoute: '/custom/path' }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it used in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adapted this rather quickly. The navigateToApp('myTestApp', { path: '#/' }) below should be using this app2 instead. Will fix.


const { navigateToApp } = await service.start(startDeps);
await navigateToApp('myTestApp', { path: '#/' });
expect(MockHistory.push).toHaveBeenCalledWith('/app/myTestApp#/', undefined);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add an assertion when # not present

* When defined, this value will be used as a default for the `path` option when calling {@link ApplicationStart.navigateToApp | navigateToApp}`,
* and will also be appended to the {@link ChromeNavLink | application navLink} in the navigation bar.
*/
defaultPath?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we mark it as @deprecated?

return url;
};

export const appendAppPath = (appBasePath: string, path: string = '') => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any tests for appendAppPath?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, this was introduced in my last commit, and I did not add proper coverage for it. Will add.

* Utility to remove trailing, leading or duplicate slashes.
* By default will only remove duplicates.
*/
export const removeSlashes = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider to move it when needed in another place

src/core/public/chrome/nav_links/nav_link.ts Show resolved Hide resolved
Comment on lines +30 to +37
const getKibanaUrl = (pathname?: string, search?: string) =>
url.format({
protocol: 'http:',
hostname: process.env.TEST_KIBANA_HOST || 'localhost',
port: process.env.TEST_KIBANA_PORT || '5620',
pathname,
search,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@pgayvallet pgayvallet merged commit aa8cb62 into elastic:master Apr 30, 2020
pgayvallet added a commit to pgayvallet/kibana that referenced this pull request Apr 30, 2020
)

* add defaultPath to `AppBase` and use it in `navigateToApp`

* add removeSlashes util

* adapt `toNavLink` to handle defaultPath

* update generated doc

* codestyle

* add FTR test

* address comments

* add tests
pgayvallet added a commit that referenced this pull request Apr 30, 2020
…64867)

* add defaultPath to `AppBase` and use it in `navigateToApp`

* add removeSlashes util

* adapt `toNavLink` to handle defaultPath

* update generated doc

* codestyle

* add FTR test

* address comments

* add tests
gmmorris added a commit to gmmorris/kibana that referenced this pull request Apr 30, 2020
* master: (42 commits)
  [Ingest] Allow aggent to send metadata compliant with ECS (elastic#64452)
  [Endpoint] Remove todos, urls to issues (elastic#64833)
  [Uptime] Remove hard coded value for monitor states histograms (elastic#64396)
  Feature/send feedback link (elastic#64845)
  [ML] Moving get filters capability to admin (elastic#64879)
  Remove edit alert button from alerts list (elastic#64643)
  [EPM] Handle constant_keyword type in KB index patterns and ES index templates (elastic#64876)
  [ML] Disable data frame anaylics clone button based on permission (elastic#64830)
  Dashboard url generator to preserve saved filters from destination dashboard (elastic#64767)
  add generic typings for SavedObjectMigrationFn (elastic#63943)
  Allow to define and update a defaultPath for applications (elastic#64498)
  [Event Log] add rel=primary to saved objects for query targets (elastic#64615)
  [Lens] Use a size of 5 for first string field in visualization (elastic#64726)
  [SIEM][Lists] Removes plugin dependencies, adds more unit tests, fixes more TypeScript types
  [Ingest] Edit datasource UI (elastic#64727)
  [Lens] Bind all time fields to the time picker (elastic#63874)
  [Lens] Use suggestion system in chart switcher for subtypes (elastic#64613)
  Improve alpha messaging (elastic#64692)
  [Ingest] Allow to enable monitoring of elastic agent (elastic#63598)
  [Metrics UI] Fix alerting when a filter query is present (elastic#64575)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:New Platform release_note:plugin_api_changes Contains a Plugin API changes section for the breaking plugin API changes section. Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.8.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow AppUpdater to change URL for app in Nav
5 participants